package cn.zyx.controller;

import cn.zyx.common.pay.weixin.ReportReqData;
import cn.zyx.common.pay.weixin.request.QrCodeRequest;
import cn.zyx.common.pay.weixin.util.RandomStringGenerator;
import cn.zyx.common.pay.weixin.util.SnowflakeIdWorker;
import cn.zyx.Config.WeixinPayProperties;
import cn.zyx.vo.R;
import com.alibaba.fastjson.JSONObject;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author zzp
 * @since 2021/12/18 18:43
 */
@RestController
public class ApiPayController {

    @GetMapping("/weixinpay")
    public R payFood(HttpServletRequest request){
        //1：开始支付,创建数据体
        JSONObject json = new JSONObject();
        //2：生成自定义定义流水号，便于自己查询和统计保证唯一即可
        String orderNo = new SnowflakeIdWorker(1, 2).nextId() + "";
        //3:获取用户openid作为支付的唯一凭证
        String openid = request.getParameter("openid");
        System.out.println(openid);
        String money = request.getParameter("money");
        System.out.println(money);
        //4：分账接收方用户Id,从session获取即可
        Integer userId = 1;
        //5：组装微信支付数据
        ReportReqData data = new ReportReqData();
        // openid
        data.setOpenid(openid);
        // 签约的类型JSAPI
        data.setTrade_type(WeixinPayProperties.WX_TYPE);
        // 微信小程序的appid
        data.setAppid(WeixinPayProperties.WX_APPID);
        // 商户id
        data.setMch_id(WeixinPayProperties.WX_MCHID);
        // 回调地址 如果是微信小程序不用配置也可以，最好配置
        data.setNotify_url(WeixinPayProperties.WX_NOTIFYPATH);
        // 配置微信支付API密钥
        data.setKey(WeixinPayProperties.WX_APPSECRET);
        // 业务数据
        data.setBody("产品支付");//
        data.setOut_trade_no(orderNo);//订单号
        //data.setProduct_id(saleFoodId+"");//商品ID
        data.setSpbill_create_ip("127.0.0.1");//ip地址
        data.setTotal_fee(money);//金额
        data.setNonce_str(RandomStringGenerator.getRandomStringByLength(32));

        json.put("userId", userId);
        json.put("type", "productpay");
        //创建订单
        String params = json.toString().replace("\"", "'");

        data.setAttach(params);

        //微信支付返回的结果
        Map<String, String> weixinMap = QrCodeRequest.submitWeixinMessage(data);

        return R.ok().data("weixinMap",weixinMap).data("xxx","xxxx");

    }



    /**
     * 元转换成分
     *
     * @param amount
     * @return
     */
    public static String getMoney(String amount) {
        if (amount == null) {
            return "";
        }
        // 金额转化为分为单位
        // 处理包含, ￥ 或者$的金额
        String currency = amount.replaceAll("\\$|\\￥|\\,", "");
        int index = currency.indexOf(".");
        int length = currency.length();
        Long amLong = 0L;
        if (index == -1) {
            amLong = Long.valueOf(currency + "00");
        } else if (length - index >= 3) {
            amLong = Long.valueOf((currency.substring(0, index + 3)).replace(".", ""));
        } else if (length - index == 2) {
            amLong = Long.valueOf((currency.substring(0, index + 2)).replace(".", "") + 0);
        } else {
            amLong = Long.valueOf((currency.substring(0, index + 1)).replace(".", "") + "00");
        }
        return amLong.toString();
    }

}

